package me.zhengjie.modules.finace_taxation.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.finace_taxation.service.FtActivityService;
import me.zhengjie.modules.finace_taxation.service.dto.FtActivityDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtActivityQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "activity管理")
@RequestMapping("/api/ft/ftActivity")
public class FtActivityController {

    private final FtActivityService ftActivityService;

    @GetMapping
    @Log("查询活动")
    @ApiOperation("查询活动")
//    @PreAuthorize("@el.check('ftActivity:list')")
    public ResponseEntity query(FtActivityQueryParam query, Pageable pageable){
        return new ResponseEntity<>(ftActivityService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增活动")
    @ApiOperation("新增活动")
//    @PreAuthorize("@el.check('ftActivity:add')")
    public ResponseEntity create(@Validated @RequestBody FtActivityDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftActivityService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改活动")
    @ApiOperation("修改活动")
//    @PreAuthorize("@el.check('ftActivity:edit')")
    public ResponseEntity update(@Validated @RequestBody FtActivityDto resources){
        ftActivityService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除活动")
    @ApiOperation("删除活动")
//    @PreAuthorize("@el.check('ftActivity:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftActivityService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出活动列表")
    @ApiOperation("导出活动列表")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtActivityQueryParam query) throws IOException {
        ftActivityService.download(ftActivityService.queryAll(query), response);
    }

}
